<?php
/**
 * File which holds the cFunctions-class
 *
 * @author Xander Hoogland <home@xanland.nl>
 * @package Classes
 */

/**
 * Classes with functions available in CMS
 *
 * @todo Get rid of the global declarations in getIncludeContents
 *
 * @author Xander Hoogland <home@xanland.nl>
 * @package Classes
 */
class cFunctions
{
   /**
    * Checks for a valid e-mailaddress
    *
    * @access public
    * @param string $sEmail
    * @return string
    */
    public static function checkMail ($sEmail)
    {
		if (version_compare (PHP_VERSION, '5.2.0') == 1)
		{
			return filter_var ($sEmail, FILTER_VALIDATE_EMAIL);
		}
		elseif (version_compare (PHP_VERSION, '5.2.0') == -1)
		{
			return ereg ("[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([.]{1}[A-Za-z0-9-]+)+", $sEmail);
		}
    }

	/**
	 * Get the included contents of a file for including as PHP
	 *
	 * @access public
	 * @global array $_CONFIG
	 * @global object $pPDO
	 * @global array $a_sRequest
	 * @global string $sTitle
	 * @param string $sFilename
	 * @param array $aArgs
	 * @return boolean
	 */
	public static function getIncludeContents ($sFilename, $aArgs = false)
    {
		global $_CONFIG, $pPDO, $a_sRequest, $sTitle, $photoAlbum;

		if (is_file ($sFilename))
		{
			ob_start ();
			include $sFilename;
			$sContents = ob_get_contents ();
			ob_end_clean ();
			return $sContents;
		}
		else
		{
			return false;
		}
    }

   /**
    * Include a module with the info which is set
    *
    * @access public
    * @param array $moduleInfo
    * @return string
    */
    public static function includeModule ($moduleInfo)
    {
		foreach ($moduleInfo as $aModuleInfo)
		{
			$aModuleInfo = str_replace ('(module::', '', str_replace (')', '', $aModuleInfo));
			$aModuleInfo = explode ('|', $aModuleInfo);
			$sModuleName = str_replace ('::', '/', $aModuleInfo[0]);
			if (isset ($aModuleInfo[1]))
			{
				$aModuleInfo = explode (',', $aModuleInfo[1]);
				foreach ($aModuleInfo as $sModuleArgs)
				{
					$sArgument = explode ('=', $sModuleArgs);
					$_ARRAY[$sArgument[0]] = $sArgument[1];
				}
				return self :: getIncludeContents ('cms/modules/' . $sModuleName . '/index.php', $_ARRAY);
			}
			else
			{
				return self :: getIncludeContents ('cms/modules/' . $sModuleName . '/index.php');
			}
		}
    }

   /**
    * Shows different vars about the server and the request and
    * (if needed) also a particular variable.
    *
    * @access public
    * @param mixed $sVar
    * @param boolean $bBoth
    * @return boolean
    */
    public static function printVars ($sVar, $bBoth = false)
    {
		echo '<pre>';

		if ($bBoth == true)
		{
			echo '$_SERVER: <br />';
			print_r ($_SERVER);
			echo '<br />$_GET: <br />';
			print_r ($_GET);
			echo '<br />$_POST: <br />';
			print_r ($_POST);
			echo '<br />$_SESSION: <br />';
			print_r ($_SESSION);
		}

		if (isset ($sVar) && !empty ($sVar))
		{
			echo '<br />$sVar: <br />';
			var_dump ($sVar);
		}

		echo '</pre>';
		return true;
    }

   /**
    *
    * Alias for printVars-method
    *
    * @access public
    * @param string $sVar
    * @param boolean $bBoth
    * @return boolean
    */
    public static function printVar ($sVar, $bBoth = false)
    {
		self::printVars ($sVar, $bBoth);
		return true;
    }

	/**
	 * Make a string of (standard) 8 characters for use in
	 * passwords or other things
	 *
	 * @access public
	 * @param int $iChrs
	 * @return string
	 */
	public static function createString($iChrs = 8)
	{
		if(!is_Numeric($iChrs))
		{
			$iChrs = 8;
		}

		$sReturn='';
		$aInteger = range('0','9');
		$aAlpha = range('a','z');
		$aAlphaC = range('A','Z');

		$aChrs = array_merge($aInteger, $aAlpha, $aAlphaC);

		for($cnt=0;$cnt<$iChrs;$cnt++)
		{
			$sReturn.= $aChrs[rand(0,count($aChrs)-1)];
		}

		return $sReturn;
	}

	/**
	 * String to send someone back so they can edit
	 * something
	 *
	 * @access public
	 * @return string
	 */
	public static function goBack()
	{
		return '<br><a href="javascript:history.go(-1)">Ga terug</a> om het te wijzigen.';
	}

	public static function hashPassword ($sPassword)
	{
		return sha1 ('just to make it salt it thingie' . md5 ($sPassword));

	}
}